package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolOperLogMapper;
import cn.da.shuai.cool.system.model.entity.log.CoolOperLog;
import cn.da.shuai.cool.system.service.service.ICoolOperLogService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 操作日志服务实现类
 */
@Service
public class CoolOperLogServiceImpl extends CoolServiceImpl<CoolOperLogMapper, CoolOperLog> implements ICoolOperLogService {

    @Override
    public void insertOperlog(CoolOperLog operLog) {
        this.save(operLog);
    }

    @Override
    public List<CoolOperLog> selectOperLogList(CoolOperLog operLog) {
        return selectOperLogList(operLog, null);
    }

    @Override
    public Page<CoolOperLog> selectOperLogPage(CoolOperLog operLog, Page<CoolOperLog> page) {
        return this.page(page, getWrapper(operLog));
    }

    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return this.removeByIds(Arrays.asList(operIds)) ? operIds.length : 0;
    }

    @Override
    public CoolOperLog selectOperLogById(Long operId) {
        return this.getById(operId);
    }

    @Override
    public void cleanOperLog() {
        this.remove(new LambdaQueryWrapper<>());
    }

    /**
     * 查询操作日志列表
     */
    private List<CoolOperLog> selectOperLogList(CoolOperLog operLog, Page<CoolOperLog> page) {
        return this.list(getWrapper(operLog));
    }

    private LambdaQueryWrapper<CoolOperLog> getWrapper(CoolOperLog operLog) {
        LambdaQueryWrapper<CoolOperLog> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(operLog.getTitle()), CoolOperLog::getTitle, operLog.getTitle())
           .eq(operLog.getBusinessType() != null, CoolOperLog::getBusinessType, operLog.getBusinessType())
           .eq(operLog.getStatus() != null, CoolOperLog::getStatus, operLog.getStatus())
           .like(StrUtil.isNotBlank(operLog.getOperName()), CoolOperLog::getOperName, operLog.getOperName())
           .between(operLog.getCreateTime() != null && operLog.getUpdateTime() != null,
                   CoolOperLog::getOperTime, operLog.getCreateTime(), operLog.getUpdateTime());
        return lqw;
    }
}